package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import primatives.PointND;

public class PointCloudReader
{
	private static final String VOXEL_EOF = "**end cloud**";
	public static final String VOXEL_FILE_EXT = ".asc";
	
	public static ArrayList<PointND> ReadFile(String filename)
	{
		System.out.print("Reading from file " + filename + "... ");
		ArrayList<PointND> ret = new ArrayList<PointND>();
		try
		{
		    BufferedReader in = new BufferedReader(new FileReader(filename));
		    String str = in.readLine(); // ignore the first line
//		    String str; // don't ignore the first line
		    while ((str = in.readLine()) != null)
		    {
		    	if (str.equals(VOXEL_EOF) == false)
		    	{
		    		String[] values = str.split(" ");
			        PointND pt = new PointND(values.length);
			        for (int i = 0; i < values.length; i++)
			        {
			        	pt.set_coord(i, Double.parseDouble(values[i]));
			        }
			        ret.add(pt);
		    	}
		    }
		    in.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		System.out.println(" done! ["+ret.size()+" points]");
		return ret;
	}
	
	public static void main(GeoTriangle args[])
	{
		String voxelFilePath = "data"+File.separator+"scan_center_1to1_fill"+VOXEL_FILE_EXT;
		ArrayList<PointND> voxels = PointCloudReader.ReadFile(voxelFilePath);
		System.out.println("Read " + voxels.size() + " voxels from " + voxelFilePath);
	}
}
